<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true" label-width="100px">
        <el-col>
          <el-form-item label="工厂">
            <el-select
              @keyup.enter="search"
              v-model="searchParam.plantId"
              placeholder="请选择工厂"
              clearable
              filterable
            >
              <el-option
                v-for="item in plantIdList"
                :key="item.id"
                :label="item.organizationCode"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="线边库">
            <el-select
              @keyup.enter="search"
              v-model="searchParam.produceStorageId"
              placeholder="请选择线边库"
              clearable
              filterable
            >
              <el-option
                v-for="item in produceStorageList"
                :key="item.id"
                :label="item.produceStorageCode"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="供应批次">
            <el-input
              v-model="searchParam.materialBatch"
              placeholder="请输入供应批次"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>
          <el-form-item label="存货编码">
            <el-input
              v-model="searchParam.materialCode"
              placeholder="请输入存货编码"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>
          <el-form-item label="库存来源">
            <el-select
              @keyup.enter="search"
              v-model="searchParam.stockFrom"
              placeholder="请选择库存来源"
              clearable
              filterable
            >
              <el-option
                v-for="item in stockFromList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否正在使用">
            <el-select
              @keyup.enter="search"
              v-model="searchParam.isFeedMaterial"
              placeholder="请选择是否正在使用"
              clearable
              filterable
            >
              <el-option
                v-for="item in statusList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否已发外协">
            <el-select
              @keyup.enter="search"
              v-model="searchParam.isOutsourcing"
              placeholder="请选择是否已发外协"
              clearable
              filterable
            >
              <el-option
                v-for="item in statusList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item>
            <el-button type="primary" @click="search"
              ><i-ep-search />搜索</el-button
            >
            <el-button @click="reset"><i-ep-refresh />重置</el-button>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="
                useDownload(
                  '/mes/mesproducingstockrecord/export',
                  '生产线边库存',
                  searchParam
                )
              "
            >
              <template #icon><i-ep-download /></template>导出</el-button
            >
          </el-form-item>
        </el-col>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        :cell-style="cellStyle"
        :header-cell-style="{ 'text-align': 'center' }"
      >
        <!--<el-table-column
          label="存货名称"
          prop="materialName"
          min-width="150"
          show-overflow-tooltip
          align="left"
        />-->

        <template v-for="item in tableColumns" :key="item">
          <TableColumn v-if="item.prop && item.isShow" :column="item">
            <template v-for="slot in Object.keys($slots)" #[slot]="scope">
              <slot :name="slot" v-bind="scope"></slot>
            </template>
          </TableColumn>
        </template>
        <!-- <el-table-column
          label="工作中心"
          prop="workCenterCode"
          min-width="100"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="存货编码"
          prop="materialCode"
          min-width="150"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="批次号"
          prop="materialBatch"
          min-width="100"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="物料条码"
          prop="materialBarcode"
          min-width="120"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="数量"
          prop="materialQuantity"
          min-width="100"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="数量单位"
          prop="quantityUnit"
          min-width="100"
          show-overflow-tooltip
          align="left"
        >
          <template #default="{ row }">
            <span>{{
              useDictionary("MeasurementUnit", row.quantityUnit)
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="工序"
          prop="processesClassification"
          min-width="100"
          show-overflow-tooltip
          align="left"
        >
          <template #default="{ row }">
            <span>{{
                useDictionary("ProcessType", row.processesClassification)
              }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="线边库"
          prop="produceStorageName"
          min-width="120"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="是否合格"
          prop="isValid"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag v-if="scope.row.isValid === 1">是</el-tag>
            <el-tag v-else>否</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="是否正在使用"
          prop="isFeedMaterial"
          min-width="120"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag v-if="scope.row.isFeedMaterial === 1">是</el-tag>
            <el-tag v-else>否</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="库存来源"
          prop="stockFrom"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag v-if="scope.row.stockFrom === 1">外购</el-tag>
            <el-tag v-else-if="scope.row.stockFrom === 0">内购</el-tag>
            <el-tag v-else>未知</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="创建人"
          prop="createdUserName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="修改时间"
          prop="lastUpdateTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="修改人"
          prop="lastUpdatedUserName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        /> -->
        <el-table-column fixed="right" label="操作" width="200" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <producestorage-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id: string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />

    <!-- 列设置 -->
    <ColSetting
      v-model:col-setting="tableColumns"
      v-model:search-setting="searchColumns"
      @search-handle="(e:boolean) => (showSearch = e)"
    />
  </div>
</template>

<script setup lang="tsx">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { ElTable } from "element-plus";

defineOptions({
  name: "MESPRODUCESTORAGE",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/mes/mesproducingstockrecord/queryByPageOutsourcing";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  materialCode: "",
  materialName: "",
  materialType: "",
  plantId: "",
  figureNumber: "",
};
// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(); // 无需二次数据处理&不传赞数也行
const plantIdList: any = ref([]);
const produceStorageList: any = ref([]);
async function plantIdListFunction() {
  const plantList = await request({
    url: "system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  plantIdList.value = plantList.data;

  const produceStorage = await request({
    url: "mes/mesproducestorage/getList",
    method: "post",
    data: {},
  });
  produceStorageList.value = produceStorage.data;
}
// const dataCallBack = ref(undefined);
// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError
);

onMounted(() => {
  getTableList();
  plantIdListFunction();
});

const addDialog: any = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

const configureDialog: any = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getConfig(id: string) {
  request({
    url: `/mdm/mdmmaterial/get/${id}`,
    method: "get",
  }).then((data) => {
    configureDialog.data = data.data;

    configureDialog.visible = true;
  });
}

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/mes/mesproducingstockrecord/get/${id}`,
    method: "get",
  }).then((data) => {
    addDialog.data = data.data;

    addDialog.visible = true;
  });
}

/** 打开物料配置表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改生产线边库存";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增生产线边库存";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开物料主数据表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "物料主数据详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
  addDialog.rulesValue = false;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function configVisible(value: boolean) {
  configureDialog.visible = value;
  if (!value) {
    configureDialog.data = {};
  }
}
const statusList = [
  { name: "是", id: "1" },
  { name: "否", id: "0" },
];
const stockFromList = [
  { name: "外购", id: "1" },
  { name: "内购", id: "0" },
];
const cellStyle = (data) => {
  if (data.row.coreState == "1") {
    return {
      color: "#000",
      background: "#FBE2E2",
    };
  }
};

const tableColumns = reactive([
  {
    prop: "workCenterCode",
    label: "工作中心",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "materialCode",
    label: "存货编码",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "materialBatch",
    label: "批次号",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "materialBarcode",
    label: "物料条码",
    minWidth: 120,
    isShow: true,
    align: "left",
  },
  {
    prop: "materialQuantity",
    label: "数量",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "quantityUnit",
    label: "数量单位",
    minWidth: 100,
    isShow: true,
    align: "left",
    render: (scope: any) =>
      useDictionary("MeasurementUnit", scope.row.quantityUnit),
  },
  {
    prop: "processesClassification",
    label: "工序",
    minWidth: 100,
    isShow: true,
    align: "left",
    render: (scope: any) =>
      useDictionary("ProcessType", scope.row.processesClassification),
  },
  {
    prop: "produceStorageName",
    label: "线边库",
    minWidth: 120,
    isShow: true,
    align: "left",
  },
  {
    prop: "isValid",
    label: "是否合格",
    minWidth: 100,
    isShow: true,
    align: "center",
    render: (scope: any) =>
      scope.row.isValid == 1 ? <el-tag>是</el-tag> : <el-tag>否</el-tag>,
  },

  {
    prop: "isFeedMaterial",
    label: "是否正在使用",
    minWidth: 120,
    isShow: true,
    align: "center",
    render: (scope: any) =>
      scope.row.isFeedMaterial == 1 ? <el-tag>是</el-tag> : <el-tag>否</el-tag>,
  },
  {
    prop: "stockFrom",
    label: "库存来源",
    minWidth: 100,
    isShow: true,
    align: "center",
    render: (scope: any) => {
      if (scope.row.stockFrom == 1) return <el-tag>外购</el-tag>;
      if (scope.row.stockFrom == 0) return <el-tag>内购</el-tag>;
      return <el-tag>未知</el-tag>;
    },
  },
  {
    prop: "createTime",
    label: "创建时间",
    minWidth: 160,
    isShow: true,
    align: "center",
  },
  {
    prop: "createdUserName",
    label: "创建人",
    minWidth: 100,
    isShow: true,
    align: "center",
  },
  {
    prop: "lastUpdateTime",
    label: "修改时间",
    minWidth: 160,
    isShow: true,
    align: "center",
  },
  {
    prop: "lastUpdatedUserName",
    label: "修改人",
    minWidth: 100,
    isShow: true,
    align: "center",
  },
]);

const searchColumns = reactive([]);
</script>
<style lang="scss" scoped></style>
